#Import library
import pandas as pd
import numpy as np
ls
#Reading excel sheet
df = pd.read_excel('Ad Hoc Exercise Job#20-024.xlsx', names='Sheet1',header=None,
usecols=range(0,3), skiprows=range(0,4))
new_columns=['University','Fall 2017', 'Fall 2018']
df.columns = new_columns
#Print first 5 rows
df.head()
# Check data types
df.dtypes
# select university data
df_university = df[0:37]
# df_university_total = df.loc[37:38,:]
# Calcualte the change rate from fall 2018
df_university[:]['Change Rate'] =(df_university[:]['Fall 2018']-df_university[:]['Fall 2017'])/df_university[:]['Fall 2017']
df_university.describe()
# Covert number of students in Fall 17 and Fall 18 to Int
df_university.loc[:,'Fall 2017']=df_university.loc[:,'Fall 2017'].astype(int)
df_university.loc[:,'Fall 2018']=df_university.loc[:,'Fall 2018'].astype(int)
df_university.dtypes
#sort value the by ascensding Change rate in percentage
df_university.sort_values(by=['Change Rate'],inplace=True)
# Reset index
df_university.reset_index(drop = True,inplace=True)
df_styled =df_university.style\
.apply(lambda x: ['color: red' if x < 0 else "color: #228B22" for x in df_university['Change Rate']],
axis =0,subset=['Change Rate'])\
.background_gradient(cmap='YlGn',subset=['Fall 2017','Fall 2018'], )\
.format({'Change Rate':"{:.2%}"})\
.set_table_styles(
[{'selector': 'tr:nth-of-type(odd)',
'props': [('background', '#eee')]},
{'selector': 'tr:nth-of-type(even)',
'props': [('background', 'white')]},
{'selector': 'th',
'props': [('background', '#949292'),
('color', 'white'),
('font-family', 'verdana')]},
{'selector': 'td',
'props': [('font-family', 'verdana')]},
]
).hide_index()
df_styled
# Export to Excel
df_styled.to_excel('Fall Semesters Comparation by Trend of Number of Student Enrollment.xlsx',sheet_name="University Enrollment", engine='openpyxl')